﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.DeviceInfo;
using BCDto.Common.DeviceInfo;
using BCDto.Website.User;
using BCEntity.Common.DeviceInfo;
using BCService.Utils;
using System;
using System.Linq;

namespace BCService.Common.DeviceInfo
{
    public class DeviceInfoService : IDeviceInfoService
    {
        private readonly IDeviceInfoData deviceInfoData;

        public DeviceInfoService(IDeviceInfoData deviceInfoData)
        {
            this.deviceInfoData = deviceInfoData;
        }

        public PageableList<DeviceInfoViewDto> GetDeviceInfoViews(string phone, string userName, string role, string appType, string osType, string model, string deviceId, bool? ascending, int pageIndex, int pageSize)
        {
            var data = this.deviceInfoData.GetDeviceInfoViews(phone, userName, role, appType, osType, model, deviceId, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
            return new PageableList<DeviceInfoViewDto>
            {
                Count = data.Item2,
                Items = data.Item1.Select(p => p.As<DeviceInfoViewDto>()),
                PageIndex = pageIndex,
                PageSize = pageSize,
                ServerTime = DateTime.Now
            };
        }

        public void UpdateAsync(UserDto user, AppType appType, DeviceInfoPutDto dto)
        {
            if (user == null)
            {
                return;
            }

            DeviceInfoEntity currentUserDeviceInfo = this.deviceInfoData.Get(user.UserId).GetAwaiter().GetResult();

            //// 获取同一设备上的所有用户
            if (currentUserDeviceInfo == null)
            {
                var deviceInfoEntity = new DeviceInfoEntity
                {
                    UserId = user.UserId,
                    AppType = appType.ToString(),
                    OSType = dto.OSType.ToString(),
                    OSVersion = dto.OSVersion,
                    Model = dto.Model,
                    DeviceId = dto.DeviceId,
                    PushType = dto.PushType.ToString(),
                    PushToken = dto.PushToken,
                    QuietMode = dto.QuietMode,
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now
                };
                currentUserDeviceInfo = this.deviceInfoData.Create(deviceInfoEntity).GetAwaiter().GetResult();
            }
            else
            {
                currentUserDeviceInfo.Model = dto.Model;
                currentUserDeviceInfo.DeviceId = dto.DeviceId;
                currentUserDeviceInfo.OSType = dto.OSType.ToString();
                currentUserDeviceInfo.OSVersion = dto.OSVersion;
                currentUserDeviceInfo.PushType = dto.PushType.ToString();
                currentUserDeviceInfo.PushToken = dto.PushToken;
                currentUserDeviceInfo.QuietMode = dto.QuietMode;
                currentUserDeviceInfo.UpdateTime = DateTime.Now;
                this.deviceInfoData.UpdateAsync(currentUserDeviceInfo);
            }

            //// 将同一设备上的其他用户更新为静音状态。
            //foreach (var item in deviceInfos)
            //{
            //    if (!item.QuietMode && item.DeviceInfoId != currentUserDeviceInfo.DeviceInfoId)
            //    {
            //        item.QuietMode = true;

            //        this.deviceInfoData.UpdateAsync(item);
            //    }
            //}
            return;
        }
    }
}
